﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SectorCreator : MonoBehaviour
{
    public float radius = 5;
    public float angle = 60;
    public void CreateSector(float radius, float angle)
    {
        this.radius = radius;
        this.angle = angle;
        var meshFilter = gameObject.GetOrAddComponent<MeshFilter>();
        meshFilter.mesh = Create(radius,90 - angle / 2,angle,10);
    }

    public void CreatePizza(float radius, float angle, float height)
    {
        this.radius = radius;
        this.angle = angle;
        var meshFilter = gameObject.GetOrAddComponent<MeshFilter>();
        meshFilter.mesh = CreateWithHeight(radius,90 - angle / 2,angle,height,10);
    }

    public void Check()
    {
        /*var meshFilter = gameObject.GetComponent<MeshFilter>();
        if (meshFilter)
        {
            var mesh = meshFilter.mesh;
        }*/
    }
    private Mesh CreateWithHeight(float radius, float startAngleDegree, float angleDegree,float height, int segments)
    {
        if (segments == 0)
        {
            segments = 1;
#if UNITY_EDITOR
            Debug.Log("segments must be larger than zero.");
#endif
        }


        Mesh mesh = new Mesh();

        var singleSideVeticesCount = 3 + segments - 1;
        var singleHeightVeticesCount = 4;
        var totalVeticesCount = singleSideVeticesCount * 2 + singleHeightVeticesCount * 3;

        int[] triangles = new int[segments * 3 * 2 + 6 * 3];
        Vector3[] vertices = new Vector3[totalVeticesCount];
        
        //uv是网格上的点对应到纹理上的某个位置的像素, 纹理是一张图片, 所以是二维
        //理解以后才发现, 之前显示出错的原因是原来的代码uv很随意的拿了顶点的计算结果
        Vector2[] uvs = new Vector2[vertices.Length];

        for (int side = 0; side < 2; side++)
        {
            float h = height / 2;
            if (side == 1)
            {
                h = -height / 2;
            }
            uvs[0 + side * singleSideVeticesCount] = new Vector2(0.5f, 0.5f);//纹理的圆心在中心
            vertices[0 + side * singleSideVeticesCount] = new Vector3(0, h, 0);//第一个点是圆心点

            float angle = Mathf.Deg2Rad * angleDegree;
            float startAngle = Mathf.Deg2Rad * startAngleDegree;
            float currAngle = angle + startAngle; //第一个三角形的起始角度
            float deltaAngle = angle / segments; //根据分段数算出每个三角形在圆心的角的角度
            for (int i = side * singleSideVeticesCount + 1; i < side * singleSideVeticesCount + singleSideVeticesCount; i++)
            {
                //圆上一点的公式: x = r*cos(angle), y = r*sin(angle)
                //根据半径和角度算出弧度上的点的位置
                float x = Mathf.Cos(currAngle);
                float y = Mathf.Sin(currAngle);
                //这里为了我的需求改到了把点算到了(x,y,0), 如果需要其他平面, 可以改成(x,0,y)或者(0,x,y)
                vertices[i] = new Vector3(x * radius, h,y * radius);
                //纹理的半径就是0.5, 圆心在0.5f, 0.5f的位置
                uvs[i] = new Vector2(x * 0.5f + 0.5f, y * 0.5f + 0.5f);
                currAngle -= deltaAngle;
            }

        
            
            for (int i = segments * 3 * side, vi = singleSideVeticesCount * side + 1; i < segments * 3 * side + segments * 3; i += 3, vi++)
            {//每个三角形都是由圆心点+两个相邻弧度上的点构成的
                triangles[i] = singleSideVeticesCount * side;
                triangles[i + 1] = vi;
                triangles[i + 2] = vi + 1;
            }
        }
        //三个边缘
        var rad1 = Mathf.Deg2Rad * (angleDegree + startAngleDegree);
        var rad2 = Mathf.Deg2Rad * startAngleDegree;
        int vIndex = singleSideVeticesCount * 2;
        int tIndex = segments * 3 * 2;
        vertices[vIndex] =  new Vector3(0, height / 2, 0);
        vertices[vIndex + 1] =  new Vector3(0, -height / 2, 0);
        vertices[vIndex + 2] =  new Vector3(Mathf.Cos(rad1) * radius, -height / 2,Mathf.Sin(rad1) * radius);
        vertices[vIndex + 3] =  new Vector3(Mathf.Cos(rad1) * radius, height / 2,Mathf.Sin(rad1) * radius);
        triangles[tIndex] = vIndex;
        triangles[tIndex + 1] = vIndex + 1;
        triangles[tIndex + 2] = vIndex + 2;
        triangles[tIndex + 3] = vIndex;
        triangles[tIndex + 4] = vIndex + 3;
        triangles[tIndex + 5] = vIndex + 2;
        uvs[vIndex] = new Vector2(1, 1);
        uvs[vIndex + 1] = new Vector2(0, 1);
        uvs[vIndex + 2] = new Vector2(0, 0);
        uvs[vIndex + 3] = new Vector2(1, 0);
        vIndex += 4;
        tIndex += 6;
        
        vertices[vIndex] =  new Vector3(0, height / 2, 0);
        vertices[vIndex + 1] =  new Vector3(0, -height / 2, 0);
        vertices[vIndex + 2] =  new Vector3(Mathf.Cos(rad2) * radius, -height / 2,Mathf.Sin(rad2) * radius);
        vertices[vIndex + 3] =  new Vector3(Mathf.Cos(rad2) * radius, height / 2,Mathf.Sin(rad2) * radius);
        triangles[tIndex] = vIndex;
        triangles[tIndex + 1] = vIndex + 1;
        triangles[tIndex + 2] = vIndex + 2;
        triangles[tIndex + 3] = vIndex;
        triangles[tIndex + 4] = vIndex + 3;
        triangles[tIndex + 5] = vIndex + 2;
        uvs[vIndex] = new Vector2(1, 1);
        uvs[vIndex + 1] = new Vector2(0, 1);
        uvs[vIndex + 2] = new Vector2(0, 0);
        uvs[vIndex + 3] = new Vector2(1, 0);
        vIndex += 4;
        tIndex += 6;
        
        vertices[vIndex] =  new Vector3(Mathf.Cos(rad1) * radius, height / 2,Mathf.Sin(rad1) * radius);
        vertices[vIndex + 1] =  new Vector3(Mathf.Cos(rad2) * radius, height / 2,Mathf.Sin(rad2) * radius);
        vertices[vIndex + 2] =  new Vector3(Mathf.Cos(rad2) * radius, -height / 2,Mathf.Sin(rad2) * radius);
        vertices[vIndex + 3] =  new Vector3(Mathf.Cos(rad1) * radius, -height / 2,Mathf.Sin(rad1) * radius);
        triangles[tIndex] = vIndex;
        triangles[tIndex + 1] = vIndex + 1;
        triangles[tIndex + 2] = vIndex + 2;
        triangles[tIndex + 3] = vIndex;
        triangles[tIndex + 4] = vIndex + 3;
        triangles[tIndex + 5] = vIndex + 2;
        uvs[vIndex] = new Vector2(1, 1);
        uvs[vIndex + 1] = new Vector2(0, 1);
        uvs[vIndex + 2] = new Vector2(0, 0);
        uvs[vIndex + 3] = new Vector2(1, 0);
        
        mesh.vertices = vertices;
        mesh.triangles = triangles;
        mesh.uv = uvs;


        return mesh;
    }
    
    private Mesh Create(float radius, float startAngleDegree, float angleDegree, int segments)
    {
        if (segments == 0)
        {
            segments = 1;
#if UNITY_EDITOR
            Debug.Log("segments must be larger than zero.");
#endif
        }


        Mesh mesh = new Mesh();
        Vector3[] vertices = new Vector3[3 + segments - 1];
        vertices[0] = new Vector3(0, 0, 0);//第一个点是圆心点


        //uv是网格上的点对应到纹理上的某个位置的像素, 纹理是一张图片, 所以是二维
        //理解以后才发现, 之前显示出错的原因是原来的代码uv很随意的拿了顶点的计算结果
        Vector2[] uvs = new Vector2[vertices.Length];
        uvs[0] = new Vector2(0.5f, 0.5f);//纹理的圆心在中心


        float angle = Mathf.Deg2Rad * angleDegree;
        float startAngle = Mathf.Deg2Rad * startAngleDegree;
        float currAngle = angle + startAngle; //第一个三角形的起始角度
        float deltaAngle = angle / segments; //根据分段数算出每个三角形在圆心的角的角度
        for (int i = 1; i < vertices.Length; i++)
        {
            //圆上一点的公式: x = r*cos(angle), y = r*sin(angle)
            //根据半径和角度算出弧度上的点的位置
            float x = Mathf.Cos(currAngle);
            float y = Mathf.Sin(currAngle);
            //这里为了我的需求改到了把点算到了(x,y,0), 如果需要其他平面, 可以改成(x,0,y)或者(0,x,y)
            vertices[i] = new Vector3(x * radius, 0,y * radius);
            //纹理的半径就是0.5, 圆心在0.5f, 0.5f的位置
            uvs[i] = new Vector2(x * 0.5f + 0.5f, y * 0.5f + 0.5f);
            currAngle -= deltaAngle;
        }


        int[] triangles = new int[segments * 3];
        for (int i = 0, vi = 1; i < triangles.Length; i += 3, vi++)
        {//每个三角形都是由圆心点+两个相邻弧度上的点构成的
            triangles[i] = 0;
            triangles[i + 1] = vi;
            triangles[i + 2] = vi + 1;
        }


        mesh.vertices = vertices;
        mesh.triangles = triangles;
        mesh.uv = uvs;


        return mesh;
    }
}
